Skip to content

fix(datepicker): allow ISO 8601 strings as inputs #7091

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 20, 2017

Conversation

mmalerba
Copy link
Contributor

@mmalerba mmalerba commented Sep 14, 2017

This is useful for apps that load a bunch of JSON data and want to stuff it into a form

fixes #6265

BREAKING CHANGES:

  • DateAdapter.getISODateString is now DateAdapter.toIso8601
  • DateAdapter now has a new abstract method DateAdapter.fromIso8601

@mmalerba mmalerba requested a review from jelbourn September 14, 2017 22:36
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Sep 14, 2017
@mmalerba mmalerba added the P1 Impacts a large percentage of users; if a workaround exists it is partial or overly painful label Sep 14, 2017
* date.
* @param date The date to get the ISO date string for.
* @returns The ISO date string date string.
*/
abstract getISODateString(date: D): string;
abstract toISODateString(date: D): string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're changing this anyway, toIsoDateString?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How opposed would you be to making these toIso8601 and fromIso8601?

return [
date.getUTCFullYear(),
this._2digit(date.getUTCMonth() + 1),
this._2digit(date.getUTCDate())
].join('-');
}

fromISODateString(iso8601String: string): Date | null {
if (iso8601String.match(ISO_8601_REGEX)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to do the check first? (comment?)

* Attempts to coerce a property to a date by parsing it as a ISO 8601 string. If not a valid
* ISO 8601 string, returns the original vlaue.
*/
private _coerceDateProperty(value: any): any {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this return D?

@Input() startAt: D;
@Input()
get startAt(): D { return this._startAt; }
set startAt(value: D) { this._startAt = this._coerceDateProperty(value); }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it weird that the user could do

picker.startAt = '1985-04-12T23:20:50.52Z';
console.log(picker.startAt); // is a different type

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kind of? but its the same idea as all of the boolean and number properties that we coerce

return [
date.getUTCFullYear(),
this._2digit(date.getUTCMonth() + 1),
this._2digit(date.getUTCDate())
].join('-');
}

fromISODateString(iso8601String: string): Date | null {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this captured as part of the normal parse function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is more of a deserialization than parsing user input

* Attempts to coerce a property to a date by parsing it as a ISO 8601 string. If not a valid
* ISO 8601 string, returns the original vlaue.
*/
private _coerceDateProperty(value: any): any {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we avoid repeating this code?

* date.
* @param date The date to get the ISO date string for.
* @returns The ISO date string date string.
*/
abstract getISODateString(date: D): string;
abstract toISODateString(date: D): string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How opposed would you be to making these toIso8601 and fromIso8601?

return [
date.getUTCFullYear(),
this._2digit(date.getUTCMonth() + 1),
this._2digit(date.getUTCDate())
].join('-');
}

fromISODateString(iso8601String: string): Date | null {
if (iso8601String.match(ISO_8601_REGEX)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use either String.prototype.search or RegExp.prototype.test rather than match

@mmalerba mmalerba force-pushed the dp-iso branch 2 times, most recently from 027be57 to 9a5df88 Compare September 18, 2017 17:27
@mmalerba
Copy link
Contributor Author

comments addressed, PTAL

Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jelbourn jelbourn added pr: lgtm action: merge The PR is ready for merge by the caretaker and removed pr: needs review labels Sep 18, 2017
@jelbourn jelbourn merged commit d2ceb2c into angular:master Sep 20, 2017
@mmalerba mmalerba deleted the dp-iso branch April 3, 2018 15:19
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement P1 Impacts a large percentage of users; if a workaround exists it is partial or overly painful
Projects
None yet
Development

Successfully merging this pull request may close these issues.

datepicker: Add support for passing IOS8601 strings to datepicker regardless of the DateAdapter
3 participants